package io.renren.softpark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunlong.common.utils.PageUtils;
import com.yunlong.common.utils.Query;
import io.renren.softpark.dao.*;
import io.renren.softpark.dto.*;
import io.renren.softpark.entity.*;
import io.renren.softpark.form.SoftwareUpdateForm;
import io.renren.softpark.service.SoftwareService;
import io.renren.softpark.util.MenuUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("softwareService")
public class SoftwareServiceImpl extends ServiceImpl<SoftwareDao, SoftwareEntity> implements SoftwareService {

    @Autowired
    FeedbackDao feedbackDao;

    @Autowired
    PlatesDao platesDao;

    @Autowired
    SortDao sortDao;

    @Autowired
    ColumnDao columnDao;

    @Autowired
    SoftwareDao softwareDao;

    @Autowired
    IntroduceDao introduceDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SoftwareEntity> page = this.page(
                new Query<SoftwareEntity>().getPage(params),
                new QueryWrapper<SoftwareEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 三级菜单
     *
     * @return
     */
    public List<AddSoftwareDto> listTree() {
        List<PlatesEntity> platesEntities = platesDao.selectList(new QueryWrapper<>());
        List<SortEntity> sortEntities = sortDao.selectList(new QueryWrapper<>());
        List<ColumnEntity> columnEntities = columnDao.selectList(new QueryWrapper<>());
        List<AddSoftwareDto> collect2 = platesEntities.stream().map((p) -> {
            List<AddSoftwareDto> collect1 = sortEntities.stream().filter((s) ->
                    p.getPlateId() == s.getParentId()
            ).map((s) -> {
                List<AddSoftwareDto> collect = columnEntities.stream().filter((c) ->
                        c.getParentId() == s.getSortId()
                ).map((c) -> {
                    AddSoftwareDto addSoftwareDto = new AddSoftwareDto();
                    addSoftwareDto.setMenuId(c.getColumnId());
                    addSoftwareDto.setMenuName(c.getColumnName());
                    return addSoftwareDto;
                }).collect(Collectors.toList());
                AddSoftwareDto addSoftwareDto = new AddSoftwareDto();
                addSoftwareDto.setMenuName(s.getSortName());
                addSoftwareDto.setChilds(collect);
                return addSoftwareDto;
            }).collect(Collectors.toList());
            AddSoftwareDto addSoftwareDto = new AddSoftwareDto();
            addSoftwareDto.setMenuName(p.getPlateName());
            addSoftwareDto.setChilds(collect1);
            return addSoftwareDto;
        }).collect(Collectors.toList());
        return collect2;
    }

    /**
     * 根据软件id返回软件的详细信息
     *
     * @param softId
     * @return
     */
    @Override
    public SoftwareDto getDetailData(String softId) {
        List<PlatesEntity> platesEntitiesList = platesDao.selectList(new QueryWrapper<PlatesEntity>());
        List<SortEntity> sortEntityList = sortDao.selectList(new QueryWrapper<SortEntity>());
        List<ColumnEntity> columnEntityList = columnDao.selectList(new QueryWrapper<ColumnEntity>());
        SoftwareEntity softwareEntity = softwareDao.selectBySoftId(softId);
        System.out.println(softwareEntity);
        SoftwareDto softwareDto = new SoftwareDto();
        softwareDto.setSoftId(softwareEntity.getSoftId());
        softwareDto.setSoftLogo(softwareEntity.getSoftLogo());
        softwareDto.setSoftName(softwareEntity.getSoftName());
        softwareDto.setShowPic(softwareEntity.getShowPic());
        List<String> parentId = new ArrayList<>();
        parentId.add("0");
        parentId.add("1");
        parentId.add(softwareEntity.getParentId());
        softwareDto.setParentId(parentId);
        softwareDto.setParentList(MenuUtil.getThreeLevelMenu(platesEntitiesList, sortEntityList, columnEntityList));

        softwareDto.setLanguage(softwareEntity.getLanguage());
        softwareDto.setSoftSize(softwareEntity.getSoftSize());
        softwareDto.setSoftAddr(softwareEntity.getSoftAddr());
        softwareDto.setEdition(softwareEntity.getEdition());
        softwareDto.setDownloads(softwareEntity.getDownloads());
        softwareDto.setPlatform(softwareEntity.getPlatform());
        softwareDto.setVerify(softwareEntity.getVerify());
        softwareDto.setUserId(softwareEntity.getUserId());
        softwareDto.setBriefIntro(softwareEntity.getBriefIntro());


        softwareDto.setInstallProRight(softwareEntity.getInstallProright());
        List<FeedbackEntity> feedbackEntityList = feedbackDao.selectList(new QueryWrapper<FeedbackEntity>().eq("SOFT_ID", softId));
        List<FeedbackDto> feedbackDtos = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (FeedbackEntity feedbackEntity : feedbackEntityList) {
            FeedbackDto feedbackDto = new FeedbackDto();
            feedbackDto.setContent(feedbackEntity.getContent());
            feedbackDto.setTime(" ");
                    //simpleDateFormat.format(feedbackEntity.getTime()));
            feedbackDto.setUserId(feedbackEntity.getUserId());
            feedbackDtos.add(feedbackDto);
        }
        softwareDto.setFeedbackList(feedbackDtos);

        softwareDto.setCreateTime(" ");
                //softwareEntity.getCreateTime().toString());
        softwareDto.setUpdateTime(" ");
                //softwareEntity.getUpdateTime().toString());
        return softwareDto;

    }

    @Override
    public void updateDetailData(SoftwareUpdateForm softwareUpdateForm) {
        SoftwareEntity softwareEntity = softwareDao.selectBySoftId(softwareUpdateForm.getSoftId());
        //更新软件信息
        softwareEntity.setSoftId(softwareUpdateForm.getSoftId());
        softwareEntity.setSoftName(softwareUpdateForm.getSoftName());
        softwareEntity.setSoftLogo(softwareUpdateForm.getSoftLogo());
        softwareEntity.setShowPic(softwareUpdateForm.getShowPic());
        softwareEntity.setParentId(softwareUpdateForm.getParentId());
        softwareEntity.setLanguage(softwareUpdateForm.getLanguage());
        softwareEntity.setSoftSize(softwareUpdateForm.getSoftSize());
        softwareEntity.setEdition(softwareUpdateForm.getEdition());
        softwareEntity.setPlatform(softwareUpdateForm.getPlatform());
        softwareEntity.setUserId(softwareUpdateForm.getUserId());
        Timestamp createTime = softwareUpdateForm.getCreateTime();
        //softwareEntity.setCreateTime(createTime);
        Timestamp updateTime = softwareUpdateForm.getUpdateTime();
        //softwareEntity.setUpdateTime(updateTime);
        softwareEntity.setDownloads(softwareUpdateForm.getDownloads());
        softwareEntity.setSoftAddr(softwareUpdateForm.getSoftAddr());
        softwareEntity.setVerify(softwareUpdateForm.getVerify());
        softwareEntity.setBriefIntro(softwareUpdateForm.getBriefIntro());
        //更新安装步骤
        softwareEntity.setInstallProleft(softwareUpdateForm.getInstallProLeft());
        softwareEntity.setInstallProright(softwareUpdateForm.getInstallProRight());
        softwareDao.update(softwareEntity,new QueryWrapper<SoftwareEntity>().eq("SOFT_ID",softwareEntity.getSoftId()));
    }

    @Override
    public List<VerifySoftwareDto> judgeVerify(List<VerifySoftwareDto> data) {
        List<VerifySoftwareDto> list = new ArrayList<>();
        for (VerifySoftwareDto verifySoftwareDto:data){
            if (!verifySoftwareDto.getVerify().equals(1)){
                list.add(verifySoftwareDto);
            }
        }
        return list;
    }

    @Override
    public List<VerifySoftwareDto> verifyColumn(Map<String, Object> params) {
        IPage<SoftwareEntity> page = this.page(
                new Query<SoftwareEntity>().getPage(params),
                new QueryWrapper<>()
        );
        List<AddSoftwareDto> addSoftwareDtos = listTree();
        List<SoftwareEntity> records = page.getRecords();
        List<VerifySoftwareDto> collect = records.stream().map((r) -> {
            VerifySoftwareDto v = new VerifySoftwareDto();
            BeanUtils.copyProperties(r, v);
            for (int i = 0; i < addSoftwareDtos.size(); i++) {
                for (int j = 0; j < addSoftwareDtos.get(i).getChilds().size(); j++) {
                    for (int k = 0; k < addSoftwareDtos.get(i).getChilds().get(j).getChilds().size(); k++) {
                        if (addSoftwareDtos.get(i).getChilds().get(j).getChilds().get(k).getMenuId().equals(r.getParentId())) {
                            v.setColumnName(addSoftwareDtos.get(i).getChilds().get(j).getChilds().get(k).getMenuName());
                            MenuPath menuPath = new MenuPath();
                            menuPath.setIndex1(i);
                            menuPath.setIndex2(j);
                            menuPath.setIndex3(r.getParentId().toString());
                            v.setMenuPath(menuPath);
                        }
                    }
                }
            }
            return v;
        }).collect(Collectors.toList());
        return collect;
    }

}